<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\RecoveryWeight;
use app\model\RecoveryCategory;

class RecoveryweightController extends Base
{
    public function list()
    {
        $weid = weid();
        $query = RecoveryWeight::where(['weid' => $weid]);

        $data = $query->order('sort asc,id asc')->select()->toArray();

        foreach ($data as &$vo) {
            if ($vo['begin_weight'] == '0') {
                $vo['title'] = $vo['end_weight'] . '公斤以下';
            } else if ($vo['end_weight'] == '0') {
                $vo['title'] = $vo['begin_weight'] . '公斤以上';
            } else {
                $vo['title'] = $vo['begin_weight'] . '-' . $vo['end_weight'];
            }
        }

        return $this->json(['data' => $data]);
    }

    public function amount()
    {
        $weight_id = input('post.weight_id', '', 'intval');
        $category_id = input('post.category_id', '', 'intval');

        $RecoveryWeight = RecoveryWeight::find($weight_id)?->toArray();
        $RecoveryCategory = RecoveryCategory::find($category_id)?->toArray();

        if ($RecoveryCategory['price']) {
            $RecoveryCategory['price'] = 1;
        }

        if ($RecoveryWeight['begin_weight'] == '0') {
            $data['amount'] = 1 * $RecoveryCategory['price'] . '元 ~ ' . $RecoveryWeight['end_weight'] * $RecoveryCategory['price']. '元';
        } else if ($RecoveryWeight['end_weight'] == '0') {
            $data['amount'] = $RecoveryWeight['begin_weight'] * $RecoveryCategory['price'] . '元以上';
        } else {
            $data['amount'] = $RecoveryWeight['begin_weight'] * $RecoveryCategory['price'] . '元 ~ ' . $RecoveryWeight['end_weight'] * $RecoveryCategory['price']. '元';
        }

        return $this->json(['data' => $data]);
    }
}
